# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
import random
from selenium.webdriver.support.ui import WebDriverWait
from LBT.Resource.LoginAtt import LoginAtt


class login(lbtlogin, LoginAtt):
    '''登录相关所有用例
    '''

    @lbtlogin.catchfun
    def test_openLoginUrl(self):
        '''
        打开登录页面并校验
        20190330duvee
        :return:
        '''
        self.docstr = "打开登录页面并校验"
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        self.assertIn(dr.current_url, self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url,
                      self.lbt_welcome_mainpage_login_errormessage)

    @lbtlogin.catchfun
    def test_empty_user_pass(self):
        '''
        1.不输入用户名和密码，点击登录，判断是否光标停留在用户名,验证url和文字
        作者：杨锦秀
        20190330duvee
        :return:
        '''
        self.docstr = "输入用户名和密码，点击登录，判断是否光标停留在用户名,验证url和文字"
        self.test_openLoginUrl()
        dr = self.driver
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_loginbtn_id).click()
        id = dr.switch_to.active_element.get_attribute("id")
        self.assertEqual(id, self.login_user_id)
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, dr.current_url)
        getinfo = dr.find_element_by_class_name(self.login_title_class_name).text
        self.assertEqual(self.login_title_text, getinfo)

    @lbtlogin.catchfun
    def test_empty_user_error_pass(self):
        '''
        3.用户名空，输入密码，点击登录，判断是否光标停留在用户名，验证url和文字
        作者：杨锦秀
        :return:
        '''
        self.docstr = "用户名空，输入密码，点击登录，判断是否光标停留在用户名，验证url和文字"
        self.test_openLoginUrl()
        dr = self.driver
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.login_error_pass)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        id = dr.switch_to.active_element.get_attribute("id")
        self.assertEqual(id, self.login_user_id)
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, dr.current_url)
        getinfo = dr.find_element_by_class_name(self.login_title_class_name).text
        self.assertEqual(self.login_title_text, getinfo)
        self.assertEqual(id, self.login_user_id)
        self.assertIn(self.lbt_welcome_mainpage_login_url, dr.current_url)

    @lbtlogin.catchfun
    def test_error_user_empty_pass(self):
        '''
        错误用户名，密码为空，判断是否光标停留在密码
        author:duvee 20190330
        :return:
        '''
        self.docstr = "错误用户名，密码为空，判断是否光标停留在密码"
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys('abcddd')
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_loginbtn_id).click()
        id = dr.switch_to.active_element.get_attribute("id")
        self.assertEqual(id, self.login_pass_id)

    @lbtlogin.catchfun
    def test_correct_user_error_pass(self):
        '''
       5.正确用户名，错误密码，验证弹框文字：用户名或密码错误
       作者：杨锦秀
        :return:
        '''
        self.docstr ='正确用户名，错误密码，验证弹框文字：用户名或密码错误'
        self.test_openLoginUrl()
        dr = self.driver
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserName)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.login_error_pass)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        alertInfo = self.get_alert_text(dr)
        self.assertEqual(self.login_alert_text, alertInfo)

    @lbtlogin.catchfun
    def test_correct_user_error_pass_IdeCode(self):
        '''
       7.三次输入错误用户名密码，点击登录后，弹出验证码框，验证验证码文本框
       作者：杨锦秀
        :return:
        '''
        self.docstr ='三次输入错误用户名密码，点击登录后，弹出验证码框，验证验证码文本框'
        self.test_correct_user_error_pass()
        dr = self.driver
        dr.switch_to.alert.accept()  # 关闭警告窗口
        self.test_correct_user_error_pass()
        dr.switch_to.alert.accept()
        self.openUrl()
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        getinfo = dr.find_element_by_id(self.login_IdeCode_id).get_attribute(self.login_IdeCode_attribute)
        self.assertEqual(self.login_IdeCode_text, getinfo)  # 判断弹出了验证码框，默认显示值为‘验证码’

    @lbtlogin.catchfun
    def test_muti_school_chioceSchool(self):
        '''
        9.1 多个学校，进入选择学校界面,供后面调用
        作者：杨锦秀
        :return:
        '''
        self.docstr ="多个学校，进入选择学校界面,供后面调用"
        self.test_openLoginUrl()
        dr = self.driver
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserNameForMutiSchool)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtUserPasswordForMutiSchool)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        self.assertTrue(dr.find_element_by_id(self.chioceSchool_schoolName_id).is_displayed())  # 判断是否进入到了选择学校页面

    @lbtlogin.catchfun
    def test_muti_school_quit(self):
        '''
        9.2 多个学校，在此页面直接退出
        作者：杨锦秀
        :return:
        '''
        self.docstr ='多个学校，在此页面直接退出'
        self.test_muti_school_chioceSchool()
        dr = self.driver
        dr.find_element_by_class_name(self.chioceSchool_quit_class_name).click()
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, dr.current_url)
        getinfo = dr.find_element_by_class_name(self.login_title_class_name).text
        self.assertEqual(self.login_title_text, getinfo)

    @lbtlogin.catchfun
    def test_muti_school_Login_and_quit(self):
        '''
       9.2 多个学校，登录成功后，退出
       作者：杨锦秀
        :return:
        '''
        self.docstr='多个学校，登录成功后，退出'
        self.test_muti_school_chioceSchool()
        dr = self.driver
        getinfo = dr.find_elements_by_xpath(self.chioceSchool_FirstSchool_xpath)[0].text  # 判断登录成功后显示学校为所选学校时使用
        dr.find_elements_by_xpath(self.chioceSchool_FirstSchool_xpath)[0].click()
        self.webdriverwait(dr,dr.find_element_by_class_name(self.FirstPage_name_class_name))
        self.assertEqual(self.lbtUserRealNameForMutiSchool,
                         dr.find_element_by_class_name(self.FirstPage_name_class_name).text)  # 判断名字是否匹配
        self.assertEqual(getinfo, dr.find_element_by_xpath(self.FirstPage_SchoolName_xpath).text)  # 判断学校是否匹配
        dr.find_element_by_class_name(self.FirstPage_name_class_name).click()
        dr.find_element_by_xpath(self.FirstPage_quit_xpath).click()
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, dr.current_url)
        getinfo = dr.find_element_by_class_name(self.login_title_class_name).text
        self.assertEqual(self.login_title_text, getinfo)

    @lbtlogin.catchfun
    def test_single_school_Login_and_quit(self):
        '''
        单个学校，登录成功后，退出
        20190330duvee
        :return:
        '''
        self.docstr ='单个学校，登录成功后，退出'
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserNameForSingleSchool)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtPasswordForSingleSchool)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        self.assertEqual(self.lbtRealNameForSingleSchool, dr.find_element_by_class_name('name').text)  # 判断是否名字匹配
        dr.find_element_by_class_name(self.FirstPage_name_class_name).click()
        dr.find_element_by_xpath(self.FirstPage_quit_xpath).click()
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, dr.current_url)

    '''
    @lbtlogin.catchfun
    def atest_number_password_Login_and_CloseTips(self):
        密码纯数字，登录成功后，弹出提示信息，关闭提示信息，
        用例取消，因为后台密码已经加密了。
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserNameForNumPassword)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtPasswordForNumPassword)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        self.assertTrue(dr.find_element_by_id(self.chioceSchool_schoolName_id).is_displayed())  # 判断是否进入到了选择学校页面
        dr.find_elements_by_xpath(self.chioceSchool_FirstSchool_xpath)[0].click()
        self.assertEqual(self.lbtRealNameForNumPassword, dr.find_element_by_class_name(self.FirstPage_name_class_name).text)  # 判断是否名字匹配
        IsDisplay = dr.find_element_by_class_name('layui-layer-title').is_displayed()  # 弹出提示框
        self.assertTrue(IsDisplay)
        getinfo = dr.find_element_by_class_name('layui-layer-content')
        self.assertEqual(getinfo.text, '您的密码过于简单，为了账户安全，建议您尽快修改密码！')
        dr.find_element_by_class_name('layui-layer-setwin').click()  # 关闭提示框
    '''
    '''
    @lbtlogin.catchfun
    def atest_number_password_Login_and_OpenModifyPass(self):
        密码纯数字，登录成功后，弹出提示信息，点去修改按钮至密码修改页
        用例取消，因为后台密码已经加密了。
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserNameForNumPassword)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtPasswordForNumPassword)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        self.assertTrue(dr.find_element_by_id("schoolName").is_displayed())  # 判断是否进入到了选择学校页面
        dr.find_elements_by_xpath("//div[@id='schoolName']/button[@class='btn_rolse']")[0].click()
        self.assertEqual(self.lbtRealNameForNumPassword, dr.find_element_by_class_name('name').text)  # 判断是否名字匹配
        IsDisplay = dr.find_element_by_class_name('layui-layer-title').is_displayed()  # 弹出提示框
        self.assertTrue(IsDisplay)
        getinfo = dr.find_element_by_class_name('layui-layer-content')
        self.assertEqual(getinfo.text, '您的密码过于简单，为了账户安全，建议您尽快修改密码！')
        mu=moduleOper(dr)
        mu.openNewFrame(dr.find_element_by_class_name('layui-layer-btn0'))
        #dr.find_element_by_class_name('layui-layer-btn0').click()  # 点击去修改
        getinfo2=dr.find_element_by_xpath("//div[@id='divoldSecCode']/label")
        self.assertEqual(getinfo2.text,'*旧密码：')
    '''

    @lbtlogin.catchfun
    def test_correct_user_empty_pass(self):
        '''
          mk
       输入用户名不输入密码，点击登录，验证文字及url
        :return:

        '''
        self.docstr='输入用户名不输入密码，点击登录，验证文字及url'
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()

        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserName)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_loginbtn_id).click()
        self.assertEqual(self.loginurl, dr.current_url)
        self.assertEqual(self.login_title_text, dr.find_element_by_class_name(self.login_title_class_name).text)

    @lbtlogin.catchfun
    def test_error_user_error_pass(self):
        '''
          mk
       输入错误用户名，错误密码，点击登录
       验证弹框文字
        :return:

        '''
        self.docstr ='输入错误用户名，错误密码，点击登录'
        self.openUrl()
        dr = self.driver

        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserName)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtPasswordForNumPassword)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        alertInfo = self.get_alert_text(dr)  # 弹出的“用户名或密码错误的”alert
        self.assertEqual(self.login_alert_text, alertInfo)

    @lbtlogin.catchfun
    def test_error_user_correct_pass(self):
        '''
          mk
       输入错误用户名，正确密码，点击登录
       验证弹框文字
        :return:

        '''
        self.docstr='输入错误用户名，正确密码，点击登录'
        self.openUrl()
        dr = self.driver
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.lbtUserName)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.lbtPasswordForNumPassword)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        alertInfo = self.get_alert_text(dr)
        self.assertEqual(self.login_alert_text, alertInfo)

    @lbtlogin.catchfun
    def test_CSH_user__pass(self):
        '''
          mk
       单园所用户登陆。用户名密码实现参数化

        :return:

        '''
        self.docstr ='单园所用户登陆。用户名密码实现参数化'
        self.openUrl()
        dr = self.driver
        t = random.randint(0, 1)
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.login_user_pass_list[t]['user'])  # 随机取一个用户
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.login_user_pass_list[t]['pass'])
        dr.find_element_by_id(self.login_loginbtn_id).click()
        WebDriverWait(dr, 30).until(
            lambda dr: dr.find_element_by_id(self.login_successmainpageframe_id).is_displayed())
        iframe = dr.find_element_by_id(self.login_successmainpageframe_id)
        dr.switch_to.frame(iframe)
        alertInfo = dr.find_element_by_class_name(self.login_success_welcome_class).text
        self.assertEqual(self.login_main_text, alertInfo)

    @lbtlogin.catchfun
    def test_CSH_Teacheruser__pass(self):
        '''
          mk
       普通老师角色登录，提示无权限用户名密码实现参数化

        :return:
        '''
        self.docstr ='普通老师角色登录，提示无权限用户名密码实现参数化'
        self.openUrl()
        dr = self.driver
        t = random.randint(0, 1)
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.No_authority_teacher_list[t]['user'])  # 随机取一个用户
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.No_authority_teacher_list[t]['pass'])
        dr.find_element_by_id(self.login_loginbtn_id).click()
        alertInfo = self.get_alert_text(dr)
        self.assertEqual(self.login_authority_text, alertInfo)
